<?php

class MemberActivityAction extends MemberBaseAction
{

    public function _empty()
    {
        $map['memberId'] = $_SESSION[C('USER_AUTH_KEY')];
        
        $Activity = D("Activity")->where($map)->order('posttime desc')->limit(10)->select();
        $this->assign("Activity", $Activity);
        $this->assign("title", "我的活动");
        $this->display("Memberactivity:list");
    }

    public function add()
    {
        $cats = $this->_getCateOption();
        $this->assign("cats", $cats);
        $this->assign('memberId',$_SESSION[C('USER_AUTH_KEY')]);
        $this->assign("dsp", "add");
        $con['type'] = '1';
        $provinces = D('Region')->where($con)->select();
        $this->assign("provinces",$provinces);

        $this->display("Memberactivity:index");
    }

    public function doadd()
    {

        $data = $_POST;

        
        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        if (!$data['cateId']){
            $this->error("如果还没有活动分类，请先添加活动分类！");
        }
            
        
        if (!empty($_FILES['attachment']['name'])){
             $data['attachment'] = $this->_upload("Activity", true, 360, 240);
        }
           
        $data['posttime'] = time();
        $data['memberId'] = $_SESSION[C('USER_AUTH_KEY')];
       // $data['regionId'] = '5';;//Cookie::get('city');
//        Load('extend');
//        import('ORG.Net.IpLocation');
//        $ip = new Iplocation(); //新建一个IP查询对象
//        $location = $ip->getlocation(); //
//        $ipadress = iconv("GBK", "UTF-8", $location['country']);
//        $data['ipAddress'] = $ipadress;
        $data['regionId'] = 5;// $_POST['region'];

//
//        Vendor('Phone.index');
//        $phone = $_POST['phone'];
//        $phoneClass = new phone($phone);
//        $data['phoneAddress'] = $phoneClass->getphone();
          $Activity = M("Activity");

          if ($Activity->add($data)) {
                $this->assign("jumpUrl", "__URL__/import");
                $this->success("发布成功！");
            } else {
                $this->error("发布失败！");
                echo $Activity->getLastSql();
            }

    }

    public function edit()
    {

        if ($_GET['aid']) {
            $Activity = D("Activity")->getByAid($_GET['aid']);

            $cats = D("Categroy")->select();
            $this->assign("cats", $cats);
            $this->assign($Activity);
            $this->assign("dsp", "edit");
            $this->display("Public:Activity");
        } else {
            $this->assign("jumpUrl", "__URL__");
            $this->error("数据不存在！");
        }
    }

    public function doedit()
    {
        $data = $_POST;
        $data['parameters'] = nl2br($data['parameters']);
        if (!empty($_FILES['attachment']['name']))
            $data['attachment'] = $this->_upload("Activity", true, 360, 240, true);
        $data['postdate'] = strtotime($_POST['postdate']);
        if (D("Activity")->save($data)) {
            $this->success("修改成功！");
        } else {
            $this->error("资料无改变或修改失败！");
        }
    }

    public function getregion() {
        $condition->parentId = $_POST['cityid'];
        $region =  D("Region")->where($condition)->select();
        $this->assign("regions",$region);
        $this->display("Public:getregion");
    }
    public function getcity() {

        $condition->parentId = $_POST['provinceid'];
        $city  = D("Region")->where($condition)->select();
        $this->assign("citys",$city);
        $this->display("Public:getcity");

    }

    public function import()
    {
        $this->display("Member:friendimport");
    }
    public function doimport()
    {
        $data = $_POST;
        //require_once COMMON_PATH .'getfriends/gmail/settings.php';
        //require_once COMMON_PATH .'getfriends/gmail/gmail.php';
        //$res = get_contacts($data['username'], $data['password']);
        //print_r($res);exit;
        include COMMON_PATH . 'getfriends/friendfactory.php';
        $friends = friendFactory::factory(trim($data['username']), trim($data['password']), trim($data['mailtype']));
        $this->assign('res', $friends);
        $this->display("Member:friendlist");
    }
    /**
     * 给该好友邮箱生成邮件加进数据库
     */
    public function doinsertimport()
    {
        $data = $_POST;
        $friend = array();
        foreach ($data['friendlist'] as $key => $value) {
            $friend[] = explode(',', $value);
        }

        $data['id'] = $_SESSION[C('USER_AUTH_KEY')];
        $Member = D("Member")->getByid($data['id']);
        if (!empty($Member['friends'])) {
            $old = array();
            $old = unserialize($Member['friends']);
            $friends = array_merge($old, $friend);
        } else {
            $friends = $friend;
        }
        $data['friends'] = serialize($friend);
        if (D("Member")->save($data)) {
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
    }
}

?>